import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisDemoSet {
    public static void test1(Jedis jedis) {
        System.out.println("sadd 和 smembers");
        jedis.flushAll();

        jedis.sadd("key", "1", "2", "3");
        Set<String> sets = jedis.smembers("key");
        System.out.println(sets);
    }

    public static void test2(Jedis jedis) {
        System.out.println("sismember");
        jedis.flushAll();

        jedis.sadd("key", "1", "2", "3");
        boolean result = jedis.sismember("key", "1");
        System.out.println("result: " + result);
    }

    public static void test3(Jedis jedis) {
        System.out.println("scard");
        jedis.flushAll();

        jedis.sadd("key", "1", "2", "3");
        long result = jedis.scard("key");
        System.out.println("result: " + result);
    }

    public static void test4(Jedis jedis) {
        System.out.println("spop");
        jedis.flushAll();

        jedis.sadd("key", "1", "2", "3");
        String value = jedis.spop("key");
        System.out.println("value: " + value);
    }

    public static void test5(Jedis jedis) {
        System.out.println("sinter");
        jedis.flushAll();

        jedis.sadd("key1", "1", "2", "3");
        jedis.sadd("key2", "2", "3", "4");

        Set<String> sinter = jedis.sinter("key1", "key2");
        System.out.println(sinter);
    }

    public static void test6(Jedis jedis) {
        System.out.println("sinterstore");
        jedis.flushAll();

        jedis.sadd("key1", "1", "2", "3");
        jedis.sadd("key2", "2", "3", "4");

        long result = jedis.sinterstore("key3", "key1", "key2");
        System.out.println("result: " + result);

        Set<String> key3 = jedis.smembers("key3");
        System.out.println(key3);
    }

    public static void test7(Jedis jedis) {
        System.out.println("srem");
        jedis.flushAll();

        jedis.sadd("key", "1", "2", "3");
        long result = jedis.srem("key", "1", "2");
        System.out.println("result: " + result);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");

        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
//            test5(jedis);
//            test6(jedis);
            test7(jedis);
        }
    }


}
